<?php
/**
 * mock_fmw_api
 * FmwXianyuController.php
 * @author: Heze
 * Date: 2024/9/5 16:27
 */

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class FmwXianyuController extends Controller
{
    protected $baseUrl = '/api/xianyu/';

    protected $requestData = [];

    public function index(Request $request)
    {
        $url = $request->get('s');
        $methodUrl = str_replace($this->baseUrl, '', $url);
        if (!empty($methodUrl)) {
            $method = str_replace('/', '_', $methodUrl);
        } else {
            return [];
        }

        if (!method_exists($this, $method)) {
            echo sprintf("[%s] mock function not exists", $method);
            exit;
        }

        $this->requestData = $request->toArray();
        return $this->$method($request);
    }

    /**
     * @param array $data
     * @return array
     */
    protected function toResponse($data)
    {
        return [
            'msg' => 'OK',
            'code' => 0,
            'data' => $data,
        ];
    }

    protected function getCurrentDate($timestamp = '')
    {
        return !empty($timestamp) ? date('Y-m-d H:i:s', $timestamp) : date('Y-m-d H:i:s');
    }

    public function product_list()
    {
        $product = [
            "spec_type" => 1, // 1-单规格, 2-多规格
            "title" => "测试0717食品-海外",
            "product_id" => '448592974859525',
            "outer_id" => "SKUA00001",
            "original_price" => 400000,
            "price" => 400000,
            "express_fee" => 1,
            "stock" => 2,
            "product_status" => 22, // 管家商品状态，枚举值：已删除-1 待发布21 销售中22 已售罄23 手动下架31 售出下架33 自动下架36
        ];

        $product2 = $product;
        $product2['product_id'] = '441864286003077';
        $product2['spec_type'] = 2;

        $pageNo = $this->requestData['page_no'] ?? 1;
        return $this->toResponse([
            "list" => $pageNo == 1 ? [$product, $product2] : [],
            "count" => $pageNo == 1 ? 2 : 0,
            "page_no" => 1,
            "page_size" => 1
        ]);
    }

    public function product_detail()
    {
        $skuItem = [
            'sku_id' => 1725527348,
            'sku_text' => 'SKU规格111',
            'outer_id' => 'SKUA00001',
            'price' => 10000,
            'stock' => 100,
        ];

        $skuItem2 = [
            'sku_id' => 1725527346,
            'sku_text' => 'SKU规格222',
            'outer_id' => 'SKUA00002',
            'price' => 10000,
            'stock' => 200,
        ];

        $skuItem3 = [
            'sku_id' => 1725527342,
            'sku_text' => 'SKU规格333',
            'outer_id' => 'SKUA00003',
            'price' => 10000,
            'stock' => 300,
        ];

        return $this->toResponse([
            "product_id" => $this->requestData["product_id"] ?? '441864286003077',
            "title" => "商品标题",
            "outer_id" => "SKUA00001",
            "product_status" => 22, // 管家商品状态，枚举值：已删除-1 待发布21 销售中22 已售罄23 手动下架31 售出下架33 自动下架36
            "spec_type" => 2, // 1-单规格, 2-多规格
            "price" => 10000,
            "original_price" => 12000,
            "stock" => 600,
            "sku_items" => [
                $skuItem, $skuItem2, $skuItem3
            ],
        ]);
    }

    public function product_edit_stock()
    {
        return [
            "code" => 100001,
            "msg" => "操作过于频繁，请稍后重试",
            "data" => []
        ];

        return $this->toResponse([]);
    }

}
